<template>
	<view class="content">
		<u-swiper :list="banners" keyName="url" showTitle indicator :autoplay="true" circular></u-swiper>
		<!-- <u-notice-bar direction="column" mode="link" :text="notices" @click="clickNotice"></u-notice-bar> -->
		<u-grid :border="false" @click="clickGrid">
			<u-grid-item v-for="(item,index) in baseList" :key="index">
				<u-icon :customStyle="{paddingTop:14+'rpx'}" :name="item.name" :size="35"></u-icon>
				<text class="grid-text">{{item.title}}</text>
			</u-grid-item>
		</u-grid>

		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getIndex,
		toPage
	} from '@/config/api.js'
	export default {
		data() {
			return {
				banners: [],
				notices: ['好好学习，天天向上'],
				noticeList: [{
					id: 1,
					content: '好好学习，天天向上'
				}],
				baseList: [{
					id: 1,
					name: 'red-packet',
					title: '功能一',
					toPage: ''
				},
				{
					id: 2,
					name: 'integral',
					title: '功能二',
					toPage: ''
				},
				{
					id: 3,
					name: 'lock',
					title: '功能三',
					toPage: ''
				},{
					id: 4,
					name: 'star',
					title: '敬请期待',
					toPage: ''
				}]
			}
		},
		onLoad(option) {
			console.log("进入首页  ==> ",option)
			//模拟请求后台API
			this.index();
		},
		//下来刷新
		onPullDownRefresh() {
			this.index();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			index() {
				console.log("发起首页请求")
				getIndex().then((res) => {
					console.log(res);
					if (res.code == 200) {
						//设置轮播图
						this.banners = res.data.banners;
						console.log("this.banners ==> {}", this.banners)
						//设置u-grid 功能格
						if(res.data.appBusiness){
							this.baseList = res.data.appBusiness;
						}
						//设置公告
						if (res.data.notices) {
							var len = res.data.notices.length;
							if (len > 0) {
								this.noticeList = res.data.notices
								this.notices = [];
								for (var i = 0; i < len; i++) {
									this.notices.push(this.noticeList[i].content)
								}
							}
						}
					} else {
						uni.$u.toast(res.msg)
					}
				}).catch((res) => {
					uni.$u.toast(res.msg)
				})
			},
			//点击公告
			clickNotice(e) {
				let obj = this.noticeList[e];
				console.log("点击了消息", obj.id)

			},
			//点击 u-grid 功能格
			clickGrid(e) {
				let obj = this.baseList[e];
				uni.$u.toast("功能开发中，敬请期待")
				return
			}
		}
	}
</script>

<style>

</style>